package ypmits.simpledraw {
	import ypmits.utils.SimpleMath;

	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;

	/**
	 * @author Dave Lenz
	 */
	public class Square extends Sprite {

		private var _width : Number = 20;
		private var _height : Number = 20;
		private var matrix : Matrix = null;
		private var gradientcolors : Array = [ 0, 0 ];
		private var gradientalphas : Array = [ 1,1 ];
		private var ratios : Array = [ 0, 0xFF ];
		private var spreadmethod : String;
		private var interpolationmethod : String;
		private var focalpointratio : Number;
		private var border : Boolean = false;
		private var bordergraphic : Shape;
		private var borderthickness : Number;
		private var bordercolor : uint;
		private var borderalpha : Number;
		private var bordertrbl : Array;

		
		
		public function Square( width : Number, height : Number, gradientcolors : Array, gradientalphas : Array, ratios : Array, rotation : Number = 0, matrix : Matrix = null, spreadmethod : String = "pad", interpolationmethod : String = "rgb", focalpointratio : Number = 0 ) 
		{
			_width = width;
			_height = height;
			
			var tempmatrix : Matrix = new Matrix( );
			tempmatrix.createGradientBox( _width, _height, 0, 0, 0 );
			tempmatrix.rotate( SimpleMath.degreesToRadians( rotation ) );
			
			gradientcolors ? this.gradientcolors = gradientcolors : null;
			gradientalphas ? this.gradientalphas = gradientalphas : null;
			ratios ? this.ratios = ratios : null;
			matrix ? this.matrix = matrix : this.matrix = tempmatrix;
			spreadmethod ? this.spreadmethod = spreadmethod : null;
			interpolationmethod ? this.interpolationmethod = interpolationmethod : null;
			focalpointratio ? this.focalpointratio = focalpointratio : null;
			
			bordergraphic = new Shape( );
			addChild( bordergraphic );
			
			redraw( );
		}

		
		
		public function setBorder( thickness : Number = 1, color : uint = 0, alpha : Number = 1, trbl : Array = null ) : void 
		{
			border = true;
			
			borderthickness = thickness;
			bordercolor = color;
			borderalpha = alpha;
			!trbl ? bordertrbl = trbl : bordertrbl = trbl;
			
			redraw();
		}

		
		
		private function redraw() : void 
		{
			graphics.clear( );
			graphics.beginGradientFill( GradientType.LINEAR, gradientcolors, gradientalphas, ratios, matrix, spreadmethod, interpolationmethod, focalpointratio );
			graphics.drawRect( 0, 0, _width, _height );
			graphics.endFill( );
			
			if( border ) {
				bordergraphic.graphics.clear( );
				bordergraphic.graphics.beginFill( bordercolor, borderalpha );
				bordergraphic.graphics.drawRect( 0, 0, _width, _height );
				
				var t : Boolean = bordertrbl[0];
				var r : Boolean = bordertrbl[1];
				var b : Boolean = bordertrbl[2];
				var l : Boolean = bordertrbl[3];
				
				var xpos : Number = l ? borderthickness : 0;
				var ypos : Number = t ? borderthickness : 0;
				
				var w : Number = r ? (_width - (borderthickness * (l ? 2 : 1)) ) : _width;
				var h : Number = b ? _height - (borderthickness * (t ? 2 : 1)) : _height;
				
				bordergraphic.graphics.drawRect( xpos, ypos, w, h );
				bordergraphic.graphics.endFill( );
			}
		}

		
		
		override public function get width() : Number 
		{
			return _width;
		}

		
		
		override public function set width( value : Number ) : void {
			_width = value;
			redraw( );
		}

		
		
		override public function get height() : Number 
		{
			return _height;
		}

		
		
		override public function set height( value : Number ) : void {
			_height = value;
			redraw( );
		}
	}
}
